package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanAttendanceEntity;
import city.spring.modules.oldman.service.OldmanAttendanceService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人护理级别
 *
 * @author 谢金婷
 * @date 2020-08-18 14:44:39
 */
@RestController
@RequestMapping("/oldman_attendance")
public class OldmanAttendanceController extends ApiController {
    private final OldmanAttendanceService oldmanAttendanceService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanAttendanceEntity, ?>> entityOrderFields;

    public OldmanAttendanceController(OldmanAttendanceService oldmanAttendanceService) {
        this.oldmanAttendanceService = oldmanAttendanceService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanAttendanceEntity::getId);
        entityOrderFields.add(OldmanAttendanceEntity::getOldmanId);
        entityOrderFields.add(OldmanAttendanceEntity::getAttendanceId);
        entityOrderFields.add(OldmanAttendanceEntity::getAttendanceFee);
        entityOrderFields.add(OldmanAttendanceEntity::getPrivatecustomizationFee);
        entityOrderFields.add(OldmanAttendanceEntity::getTotalfree);
        entityOrderFields.add(OldmanAttendanceEntity::getDescription);
        entityOrderFields.add(OldmanAttendanceEntity::getEffectivetime);
        entityOrderFields.add(OldmanAttendanceEntity::getGmtCreate);
        entityOrderFields.add(OldmanAttendanceEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>老人护理级别</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanAttendanceEntity entity) {
        LambdaQueryChainWrapper<OldmanAttendanceEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人护理级别</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanAttendanceEntity entity) {
        LambdaQueryChainWrapper<OldmanAttendanceEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanAttendanceEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人护理级别</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanAttendanceService.getById(id));
    }

    /**
     * 添加一个 <strong>老人护理级别</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanAttendanceEntity entity) {
        oldmanAttendanceService.saveOldmanAttendance(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人护理级别</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanAttendanceEntity entity) {
        entity.setId(id);
        oldmanAttendanceService.updateOldmanAttendance(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人护理级别</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanAttendanceService.deleteOldmanAttendance(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人护理级别</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanAttendanceService.deleteOldmanAttendance(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanAttendanceEntity> buildLambdaQuery(OldmanAttendanceEntity entity) {
        LambdaQueryChainWrapper<OldmanAttendanceEntity> lambdaQuery = oldmanAttendanceService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanAttendanceEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanAttendanceEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAttendanceId()), OldmanAttendanceEntity::getAttendanceId, entity.getAttendanceId());
        lambdaQuery.eq(entity.getAttendanceFee() != null, OldmanAttendanceEntity::getAttendanceFee, entity.getAttendanceFee());
        lambdaQuery.eq(entity.getPrivatecustomizationFee() != null, OldmanAttendanceEntity::getPrivatecustomizationFee, entity.getPrivatecustomizationFee());
        lambdaQuery.eq(entity.getTotalfree() != null, OldmanAttendanceEntity::getTotalfree, entity.getTotalfree());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescription()), OldmanAttendanceEntity::getDescription, entity.getDescription());
        lambdaQuery.eq(entity.getEffectivetime() != null, OldmanAttendanceEntity::getEffectivetime, entity.getEffectivetime());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanAttendanceEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanAttendanceEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanAttendanceEntity> buildQuery(OldmanAttendanceEntity entity) {
        QueryWrapper<OldmanAttendanceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getAttendanceId()), "attendance_id", entity.getAttendanceId());
        wrapper.eq(entity.getAttendanceFee() != null, "attendance_fee", entity.getAttendanceFee());
        wrapper.eq(entity.getPrivatecustomizationFee() != null, "privatecustomization_fee", entity.getPrivatecustomizationFee());
        wrapper.eq(entity.getTotalfree() != null, "totalfree", entity.getTotalfree());
        wrapper.like(StringUtils.isNotBlank(entity.getDescription()), "description", entity.getDescription());
        wrapper.eq(entity.getEffectivetime() != null, "effectivetime", entity.getEffectivetime());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 根据老人id获取护理信息
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("/getAttendanceInfoByoldmanId")
    public Object getAttendanceInfoByoldmanId(String oldmanId) {
        return success(oldmanAttendanceService.getAttendanceInfoByoldmanId(oldmanId));
    }

    /**
     * 根据老人id获取护理信息
     *
     * @param personId
     * @return
     */
    @GetMapping("/getAttendanceByoPersonId/{personId}")
    public Object getAttendanceByPersonId(@PathVariable String personId) {
        return success(oldmanAttendanceService.getAttendanceByPersonId(personId));
    }
}
